package linearlist;

public class MySinglyLinkedList {

    /**
     * 定义头节点
     */
    private Node head = null;

    /** Initialize your data structure here. */
    public MySinglyLinkedList() {

    }

    /** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
    public int get(int index) {
        if(index<0){
            return -1;
        }
        Node node = head;
        int pos = 0;
        while (node!=null && pos!=index) {
            node  = node.next;
            pos++;
        }
        if(node==null){
            return -1;
        }
        return node.getValue();
    }

    /** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
    public void addAtHead(int val) {
        Node newNode = new Node(val);
        if(head==null){
            head = newNode;
        }else {
            newNode.next = head;
            head = newNode;
        }
    }

    /** Append a node of value val to the last element of the linked list. */
    public void addAtTail(int val) {
        Node newNode = new Node(val);
        if(head==null){
            head = newNode;
        }else {
            Node temp = head;
            while (temp.next!=null){
                temp = temp.next;
            }
            newNode.next = temp.next;
            temp.next = newNode;
        }
    }

    /** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
    public void addAtIndex(int index, int val) {
        if(index==0){
            this.addAtHead(val);
        }
        //定义新节点
        Node newNode = new Node(val);
        //找到 index-1 的节点
        Node temp = head;
        int pos = 0;
        while (temp!=null && pos!=index-1){
            temp = temp.next;
            pos++;
        }
        if(temp!=null && pos==index-1){
            newNode.next = temp.next;
            temp.next = newNode;
        }
    }

    /** Delete the index-th node in the linked list, if the index is valid. */
    public void deleteAtIndex(int index) {
        if(index==0 && head!=null){
            head = head.next;
        }
        Node temp = head;
        int pos = 0;
        while (temp!=null && pos!=index-1){
            temp = temp.next;
            pos++;
        }
        if(temp!=null && pos==index-1){
            if(temp.next!=null){
                temp.next = temp.next.next;
            }
        }
    }

    public void printAll(){
        Node node = head;
        while (node!=null){
            System.out.print(node.value +"->");
            node = node.next;
        }
        System.out.println();
    }

    private static class Node{

        int value;
        Node next;

        Node(int value){
            this.value = value;
            next = null;
        }

        int getValue(){
            return value;
        }
    }
}
